<?php
/**
 * 处理测试站的每日未完成次数
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/7
 * Time: 9:13
 */

class UndoDailyWorkController extends Yaf_Controller_Abstract
{

    function IndexAction()
    {
        $request = $this->getRequest();
        $zhuyuan_id = $request->getPost("zhuyuan_id",0);
        $user_department = $request->getPost("user_department","");
        $start_date_time = $request->getPost("kaishi_time","");
        $user_department_id = $request->getPost("user_department_id",0);
        $end_date_time = $request->getPost("jieshu_time","");
        $yiyuan_id = $request->getPost("yiyuan_id",0);
        $yiyuan_user = new YizhuHistoryModel();
        if(!($start_date_time === ""))
        {
            if(!(strtotime($start_date_time)))
            {
                Utility::apiJsonError(20030, '结束时间格式不对');
            }

        }

        if(!($end_date_time === ""))
        {
            if(!(strtotime($end_date_time)))
            {
                Utility::apiJsonError(20031, '开始时间格式不对');
            }
        }
        if(!$zhuyuan_id)
        {
            Utility::apiJsonError(20014, '住院ID不能为空');
        }
        if(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }
        if(!ctype_digit("$user_department_id"))
        {
            Utility::apiJsonError(20029, '科室ID必须是整型');
        }
        $async_result["staus"] = "1";
        $async_result["response_info"] = "数据同步成功:)";
        $data = array();//返回数据
        if(empty($start_date_time) || empty($end_date_time) || $start_date_time > $end_date_time){
            Utility::apiJsonError(20010, '时间参数错误');
        }else{
            //循环遍历日期
            $date_arr = array();
            $begintime = strtotime($start_date_time);
            $endtime = strtotime($end_date_time);
            for ($start = $begintime; $start <= $endtime; $start += 24 * 3600) {
                $date_time = date("Y-m-d", $start);
                $date_arr[$date_time] = $date_time;
            }
            //处理时间
            $start_date_time = $start_date_time." 00:00:00";
            $end_date_time = $end_date_time." 23:59:59";
            //分表设置
            if(Yaconf::get("config$yiyuan_id.fenbiao_state") == "true")
            {
                $zhuyuan_yizhu_changqi = "zhuyuan_yizhu_changqi_".$user_department_id;
                $zhuyuan_yizhu_linshi = "zhuyuan_yizhu_linshi_".$user_department_id;
                $zhuyuan_yizhu_zhixing_history = "zhuyuan_yizhu_zhixing_history_".$user_department_id;
            }
            else
            {
                $zhuyuan_yizhu_changqi = "zhuyuan_yizhu_changqi";
                $zhuyuan_yizhu_linshi = "zhuyuan_yizhu_linshi";
                $zhuyuan_yizhu_zhixing_history = "zhuyuan_yizhu_zhixing_history";
            }
            $zhuyuan_basic_info = DB::select("zhuyuan_id")
                ->from("zhuyuan_basic_info")
                ->where("zhuyuan_bingqu","=",$user_department)
                ->and_where("zhuangtai","=",'住院中')
                ->and_where("yiyuan_id","=",$yiyuan_id)
                ->execute()
                ->as_array();
            $zhuayun_bingren = "";
            foreach($zhuyuan_basic_info as $one)
            {
                if($one['zhuyuan_id'] == $zhuyuan_id)
                {
                    $zhuayun_bingren = $zhuyuan_id;
                }
            }
            //该病区没有权限查询患者
            if(empty($zhuayun_bingren)){//住院id不在该病区
                //失败
                Utility::apiJsonError(20011, '没有权限查询');
            }else{
                $chaxun = array();
                $jilu = array();
                $type = array();
                $chaxun['zhuyuan_id'] = array("EQ", $zhuayun_bingren);
                $chaxun['state'] = array("exp","in('开始执行','新添加')");
                //增加判断医嘱生成时间大于开始查询时间
                $chaxun['start_time'] = array("elt", $end_date_time);
                //医嘱表增加相应
                $zhuyuan_yizhu_changqi_data = DB::select("id","zhuyuan_id","'长期' type","content","start_time","stop_time","yongfa","yongfa_type","ciliang","shiyong_danwei","pinlv","zuhao","zhixing_state","yizhu_yongfa_type","meiri_cishu","wancheng_cishu","beizhu","xiangying_state")
                                        ->from($zhuyuan_yizhu_changqi)
                                        ->where("zhuyuan_id","=",$zhuayun_bingren)
                                        ->and_where("state","in",array("开始执行","新添加"))
                                        ->and_where("yiyuan_id","=",$yiyuan_id)
                                        ->execute()->as_array();
//                $zhuyuan_yizhu_changqi_data = M($zhuyuan_yizhu_changqi)
//                    ->field("id,zhuyuan_id,'长期' type,content,start_time,stop_time,yongfa,yongfa_type,ciliang,shiyong_danwei,pinlv,zuhao,zhixing_state,yizhu_yongfa_type,meiri_cishu,wancheng_cishu,beizhu,xiangying_state")
//                    ->where($chaxun)
//                    ->select();
                //长期医嘱处理
                //print_r($zhuyuan_yizhu_changqi_data);exit;
                if($zhuyuan_yizhu_changqi_data){
                    $zuhao_changqi = array();//组号
                    $shijian_changqi = array();
                    foreach ($zhuyuan_yizhu_changqi_data as $k => $v) {
                        $zuhao_changqi["100".$v['id']] = $v['zuhao'];
                        $data["100".$v['id']]['content'] = $v['content'];
                        $data["100".$v['id']]['pinlv'] = $v['pinlv'];
                        $data["100".$v['id']]['meiri_cishu'] = $v['meiri_cishu'];
                        $data["100".$v['id']]['zuhao'] = $v['zuhao'];
                        $data["100".$v['id']]['type'] = '长期';
                        $type[$v['zuhao']] = '长期';
                        if(!isset($jilu[$v['zuhao']])){
                            foreach ($date_arr as $k1 => $v1) {
                                $date_time = substr($v['start_time'], 0, 10);
                                if($date_time < $k1){//有效时间
                                    $jilu[$v['zuhao']][$k1] = $v['meiri_cishu'];
                                }
                            }
                        }
                    }
                    //查询历史记录
                    $history_zhuangtai = DB::select("zhixing_time","zhixing_zuhao")
                        ->from($zhuyuan_yizhu_zhixing_history)
                        ->where("zhixing_state","=","执行完毕")
                        ->and_where("zhixing_zuhao","in",$zuhao_changqi)
                        ->and_where("yiyuan_id","=",$yiyuan_id)
                        ->and_where("zhixing_time","between",array($start_date_time, $end_date_time))
                        ->order_by("zhixing_time","DESC")
                        ->execute()
                        ->as_array();
                    if($history_zhuangtai){
                        foreach ($history_zhuangtai as $k => $v) {
                            $jilu[$v['zhixing_zuhao']][substr($v['zhixing_time'], 0, 10)] -= 1;
                        }
                    }
                }
                //查询符合条件临时医嘱
                $chaxun = array();
                $chaxun['zhuyuan_id'] = array("EQ", $zhuayun_bingren);
                $chaxun['state'] = array("exp","in('开始执行','新添加')");
                //临时医嘱的有效时间
                $chaxun['xiada_time'] = array("between", array($start_date_time, $end_date_time));
                $zhuyuan_yizhu_linshi_data = DB::select("id","zhuyuan_id","'临时' type","content",array("xiada_time","start_time"),array("zhixing_time","stop_time"),"yongfa","yongfa_type","ciliang","shiyong_danwei","pinlv","zuhao","zhixing_state","yizhu_yongfa_type","meiri_cishu","wancheng_cishu","beizhu","xiangying_state")
                    ->from($zhuyuan_yizhu_linshi)
                    ->where("yiyuan_id","=",$yiyuan_id)
                    ->and_where("zhuyuan_id","=",$zhuayun_bingren)
                    ->and_where("state","in",array('开始执行','新添加'))
                    ->and_where("xiada_time","between",array($start_date_time, $end_date_time))
                    ->execute()
                    ->as_array();
//                $zhuyuan_yizhu_linshi_data = M($zhuyuan_yizhu_linshi)
//                    ->field("id,zhuyuan_id,'临时' type,content,xiada_time start_time,zhixing_time stop_time,yongfa,yongfa_type,ciliang,shiyong_danwei,pinlv,zuhao,zhixing_state,yizhu_yongfa_type,meiri_cishu,wancheng_cishu,beizhu,xiangying_state")
//                    ->where($chaxun)
//                    ->select();
                //临时医嘱处理只需要完成一次
                if($zhuyuan_yizhu_linshi_data){
                    $zuhao_linshi = array();//组号
                    $date_linshi = array();
                    $cishu_linshi = array();
                    foreach ($zhuyuan_yizhu_linshi_data as $k => $v) {
                        $zuhao_linshi[$v['zuhao']] = $v['zuhao'];
                        $data["200".$v['id']]['content'] = $v['content'];
                        $data["200".$v['id']]['pinlv'] = $v['pinlv'];
                        $data["200".$v['id']]['meiri_cishu'] = $v['meiri_cishu'];
                        $data["200".$v['id']]['zuhao'] = $v['zuhao'];
                        $data["200".$v['id']]['type'] = '临时';
                        $type[$v['zuhao']] = '临时';
                        $cishu_linshi[$v['zuhao']] = 1;//临时只做一次
                        $date_linshi[$v['zuhao']] = substr($v['start_time'], 0, 10);
                    }
                    //查询历史记录
                   //$history_tiaojian['zhixing_state'] = array("EQ", "执行完毕");
                    //$history_tiaojian['zhixing_zuhao'] = array("IN",implode(",", $zuhao_linshi));
                    $history_zhuangtai = DB::select("zhixing_time","zhixing_zuhao")
                        ->from($zhuyuan_yizhu_zhixing_history)
                        ->where("yiyuan_id","=",$yiyuan_id)
                        ->and_where("zhixing_state","=","执行完毕")
                        ->and_where("zhixing_zuhao","in",$zuhao_linshi)
                        ->order_by("zhixing_time","DESC")
                        ->execute()
                        ->as_array();
                    if($history_zhuangtai){
                        foreach ($history_zhuangtai as $k => $v) {
                            $cishu_linshi[$v['zhixing_zuhao']] = 0;
                        }
                    }
                    foreach ($cishu_linshi as $k => $v) {
                        if($v > 0){
                            $jilu[$k][$date_linshi[$k]] = 1;
                        }else{
                            $jilu[$k][$date_linshi[$k]] = 0;
                        }
                    }
                }
                $arr = array();
                foreach ($jilu as $k => $v) {
                    $j=0;
                    foreach ($v as $k1 => $v1) {
                        if($v1){
                            $arr[$k][$j]['riqi'] = $k1;
                            $arr[$k][$j]['weiwancheng_cishu'] = $v1;
                            $j++;
                        }

                    }
                }
                $result = array();
                foreach ($data as $k => $v) {
                    $result[$k]['content'] = $v['content'];
                    $result[$k]['pinlv'] = $v['pinlv'];
                    $result[$k]['meiri_cishu'] = $v['meiri_cishu'];
                    $result[$k]['zuhao'] = $v['zuhao'];
                    $result[$k]['type'] = $v['type'];
                    $result[$k]['data'] = $arr[$v['zuhao']];
                }
                $data = array();
                foreach ($result as $v) {
                    $data[] = $v;
                }
            }
        }
        $async_result["result"] = $data;
        $async_result["msg"] = "";
        $async_result["error"] = 200;
        //$async_result_json = json_encode($async_result);
        // 压缩数据
        //$async_result_json = $this->compress($async_result_json,$_REQUEST["compression_http_data"]);
        //echo $async_result_json;
        Utility::apiJsonResultExtends($async_result);
        //Utility::apiJsonResult($async_result,"",$_REQUEST["compression_http_data"]);
    }


}